const app = getApp();
const utils = require('../../../utils/utils.js');
Page({
  data: {
    show_reckon: false,
    actual_money: 0,
    brokerage: 0,
    id: 0,
    type: 0,
    info: [],
    cashoutMoney: 0,
    txEndMoney: 0,
    bindNickName: "",
    phone: "",
    money: 0, 
    dxYz_No: true,
    dxYz_Yes: false,
    lastTime: 0,
    code: 0,
    preCost: 0,
    isPreCost: false,
    getCode: null,
    appOpenid: null,
    showlist: null,
    txSxf: 0
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // 金额输入框事件
  inputBlur(e){
    let money = e.detail.value;
    if (money > 0) {
      console.log(money); 
      var info = this.data.info;
      let brokerage = (this.numMulti(money, info.txBrokerage/100)).toFixed(2);
      let brokerageSxf = (this.numMulti(money, info.costReate/100)).toFixed(2);
      let actual_money = (money - brokerage - brokerageSxf).toFixed(2);
      this.setData({
        show_reckon: true,
        actual_money: actual_money,
        brokerage: brokerage,
        money: money,
        txSxf: brokerageSxf
      });
    } else {
      money = 0;
      this.setData({
        show_reckon: true,
        actual_money: 0,
        brokerage: 0,
        txSxf: 0,
        money: 0
      });
    }
  },

  // 提现请求
  addTx() {
    let actual_money = this.data.actual_money; // 实际到账金额
    let brokerage = this.data.brokerage * 1  + this.data.txSxf * 1; // 手续费
    let cashoutMoney = this.data.cashoutMoney; // 可提现金额（已经计算好了的）
    let money = this.data.money; // 用户输入的金额
    var info = this.data.info;
    if (money == 0) {
      wx.showToast({
        title: "提现金额不能小于或者等于0",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    console.log(actual_money);
    console.log(brokerage);
    console.log(cashoutMoney);
    console.log(money);
    if (cashoutMoney < money) {
      wx.showToast({
        title: "输入的金额大于可提现金额",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    let txImgMoney = info.withdrawaling;
    if (txImgMoney > 0) {
      wx.showToast({
        title: "您存在审核中的提现订单！",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (info.todayCanTxIsNo == 1) {
      wx.showToast({
        title: "您今日提现次数已达上限",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (money < info.txMinPermit) {
      wx.showToast({
        title: "输入的金额小于最低提现金额",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    let appOpenid = this.data.appOpenid;
    if (appOpenid == null) {
      wx.showToast({
        title: "微信账号未绑定",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    // 提现申请提交
    let proxuUser = app.globalData.proxuUser;
    let that = this;
    utils.AppHttpRequest('/edit/addWithdrawal', {
      id: proxuUser.id,
      accountType: proxuUser.accountType,
      accountId: proxuUser.accountId,
      applyAmount: money,
      amount: actual_money,
      poundage: brokerage,
      type: 1,
      is_min_tx: 1 // 提现小程序提现
    }, function (res) {
        wx.hideLoading();
        if (res.code == 200) {
          wx.showToast({
            title: "申请提交成功！",
            icon: 'none',
            duration: 2000
          });
          that.loadData();
          return;
        }
    })

  },

   // 输入手机号
   inputBlurPhone(e){
    let phone = e.detail.value;
    console.log(phone)
    this.setData({
      phone: phone
    });
  },
   // 验证码绑定事件
   inputBlurCode:function(e) {
      let code = e.detail.value;
      console.log(code)
      this.setData({
        code: code
      });
    },

    getDxCode:function() {
      let phone = this.data.phone;
      if (phone == "" || phone == null) {
        wx.showToast({
          title: "请输入手机号",
          icon: 'none',
          duration: 2000
        });
        return;
      } else {
        let lastTime = this.data.lastTime;
        var timestamp = Date.parse(new Date());  
        timestamp = timestamp / 1000;
        if (timestamp - lastTime < 60) {
          wx.showToast({
            title: "您已获取验证码!",
            icon: 'none',
            duration: 2000
          });
          return;
        } else {
          let loginType = this.data.loginType;
          wx.showLoading({
            title: 'loading...',
            mask: true
          });
          if (loginType == 0) { // 0 商户登录 1 合作商登录
            loginType = 3;
          }
          let that = this;
          utils.AppHttpRequest('/common/getcode', {
            phone: phone,
            type: loginType
          }, function (res) {
              wx.hideLoading();
              console.log(res);
              if (res.code == 200) {
                // 获取验证码成功,将获取到验证码保存
                var timestamp = Date.parse(new Date());  
                timestamp = timestamp / 1000;
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                });
                that.setData({
                  getCode: res.data,
                  lastTime: timestamp
                });
              }
          })
        }
      }
    },

    queRenDx(){
      let code = this.data.code;
      if (code == 0) {
        wx.showToast({
          title: "请获取短信验证码",
          icon: 'none',
          duration: 2000
        });
        return;
      }
      let getCode = this.data.getCode;
      if (code != getCode) {
        wx.showToast({
          title: "短信验证码错误",
          icon: 'none',
          duration: 2000
        });
        return;
      }
      this.setData({
        dxYz_No: false,
        dxYz_Yes: true,
        modalName: null
      });
    },
  numMulti(num1, num2) {
    let baseNum = 0
    try {
        baseNum += num1.toString().split(".")[1].length;
    } catch (e) {
    }
    try {
        baseNum += num2.toString().split(".")[1].length;
    } catch (e) {
    }
    return Number(num1.toString().replace(".", ""))
        * Number(num2.toString().replace(".", ""))
        / Math.pow(10, baseNum);
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let proxuUser = app.globalData.proxuUser;
    console.log(JSON.stringify(proxuUser));
    if (proxuUser.accountId == null) {
      wx.reLaunch({
        url: '../../tx/login/login'
      })
    }
    let loginType = app.globalData.loginType;
    // type: 3、商户登录，1、合作商
    // loginType = 3;
    let accountId = proxuUser.accountId;
    let accountType = proxuUser.accountType;
    that.setData({
        // id: "210730AWNP4BBKGC",
        id: accountId,
        // type: 2,
        // phone: "13777571228",
        phone: proxuUser.phone,
        type: accountType,
        // loginType: 1, // 3、商户，1、合作商
        loginType: loginType
    });
    this.loadData();
  },
  // 加载提现信息
  loadData() {
    let that = this;
    let id = this.data.id;
    let type = this.data.type;
    utils.AppHttpRequest('/common/getFinance', {
      id: id,
      type: type,
    }, function (res) {
        wx.hideLoading();
        // console.log("getFinance-->" + res);
        if (res.code == 200) {
          let info = res.data;
          let bindNickName = "";
          if (info.wxMinAppUser != null) {
            bindNickName = info.wxMinAppUser.nickName;
          }
          let cashoutMoney = info.cashoutMoney - info.frozenMoney  - info.uncollected;
          // info.withdrawaling  提现中
          if (type == 3) {
            // 是商户用户登录
            let preCose = info.preCost;
            let isPre = info.isPre;
            if (isPre == 1) {
              that.setData({
                preCost: preCose,
                isPreCost: true
              });
            }
            
            // if (preCose > 0) {
            //   // 是预付费商户
            //   that.setData({
            //     preCost: preCost,
            //     isPreCost: true
            //   });
            // }
          }
          that.setData({
            info: info,
            showlist: info.showlist,
            cashoutMoney: cashoutMoney,
            txEndMoney: info.alreadyCashOut, 
            bindNickName: bindNickName,
            appOpenid: info.appOpenid
          });
        }
    })
  },

  // 跳转到预付费列表界面
  toPreCostList:function() {
    // 跳转到预付费界面
    wx.navigateTo({
      url: '../../tx/prelist/prelist'
    });
  },

  // 提现记录列表
  toTxList(){
    // 跳转到提现记录列表页面
    wx.navigateTo({
      url: '../../tx/txlist/txList'
    });
  },
  // 跳转到授权页面
  toAuthInfo(){
    wx.navigateTo({
      url: '../../tx/txAuth/txAuth'
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow-->" + app.globalData.txUserInfo.appUserCode);
    if (app.globalData.txUserInfo.appUserCode != null) {
      let loginType = app.globalData.loginType;
      // type: 3、商户登录，1、合作商
      // loginType = 3;
      let proxuUser = app.globalData.proxuUser;
      let accountId = proxuUser.accountId;
      let accountType = proxuUser.accountType;
      this.setData({
          // id: "210414A84RBC484H",
          id: accountId,
          // type: 2,
          // phone: "15395817153",
          phone: proxuUser.phone,
          type: accountType,
          // loginType: 1, // 3、商户，1、合作商
          loginType: loginType
      });
      this.loadData();
    }
  },
  // 注销登录
  cancelLogin(){
    app.globalData.txUserInfo = null;
    app.globalData.proxuUser = null;
    app.globalData.openID = null;
    wx.reLaunch({
      url: '../../tx/login/login'
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})